<script type="text/javascript">
  function remove_state(){
    $(this).parent().remove()
  }

  function add_state(){
    $(this).parent().clone().insertAfter($(this).parents('tbody'))
  }
</script>
<table>
  <tr>
    <td>事件列表</td>
    <td>
      <table>
        <th>事件名称</th>
        <th>状态转换器</th>
        <tbody id="event_body">
          <%@machine.events.each do |event|%>
            <%f.fields_for :events,event do |event_fields|%>
              <tr>
                <td>
                  <%=event_fields.text_field :name%>
                </td>
                <td>
                  <table>
                    <th>源</th>
                    <th>目的</th>
                    <tbody id="tansition_body">
                      <tr>
                        <td>从：
                          <%@machine.states.each do |state|%>
                            <input type="checkbox" name="machine[event_attributes][<%=event.id%>][from_ids]" <%="checked" if  event.from_states.collect(&:id).include?(state.id)%>><%=state.name%>
                          <%end%>
                        </td>
                        <td>到：
                          <select name="machine[event_attributes][<%=event.id%>][][to_id]">
                            <%=options_from_collection_for_select @machine.states,:id,:name,(event.to_state.id if event.to_state)%>
                          </select>
                        </td>
                      </tr>
                    </tbody>
                  </table>
                </td>
                <td>
                  <a href="#" onclick="remove_state()">-</a>|<a href="#" onclick="add_state()">+</a>
                </td>
              </tr>
            <%end%>
          <%end%>
        </tbody>
      </table>
    </td>
  </tr>
</table>
